//
// Created by lenovo on 2024/8/12.
//

#include "JavaLister.h"

#include  "android/log.h"

#define LOG_TAG  "LUCAS"
#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

JavaLister::JavaLister(JavaVM *javaVM,JNIEnv *env)
{



    auto clazz1 = env->FindClass("com/example/c_call_java/MainActivity");
    jmethodID callbackID = env->GetStaticMethodID(clazz1, "callback", "(Ljava/lang/String;)V");
    if(callbackID==0){
        LOGD("find callback error");
        return;
    }
    LOGD("find callback ");



    mVM = javaVM;
    mEnv = env;
    mJClass = (jclass)env->NewGlobalRef(clazz1);
    mJmethod = callbackID;
}

void JavaLister::sendMessage(std::string message)
{



    JNIEnv  *env;
    if (mVM->AttachCurrentThread(&env, 0) != JNI_OK) {
        return;
    }

    jclass globalClazz = mJClass;//(jclass)env->NewGlobalRef(mJClass);

    jstring result = env->NewStringUTF(message.c_str());
//    mEnv->CallStaticVoidMethod(mJClass,mJmethod, result);

    env->CallStaticVoidMethod(globalClazz,mJmethod, result);
    mVM->DetachCurrentThread();

}